package com.zhengxyou.jetpackdemo.paging;

import com.zhengxyou.jetpackdemo.room.AppDatabase;
import com.zhengxyou.jetpackdemo.room.dao.UserDao;
import com.zhengxyou.jetpackdemo.room.entity.User;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;

public class PagingViewModel extends ViewModel {
    private UserDao userDao;
    public final LiveData<PagedList<User>> userList;

    public PagingViewModel() {
        this.userDao = AppDatabase.getInstance().userDao();

        PagedList.Config myPagingConfig = new PagedList.Config.Builder()
                .setPageSize(20)
                .setPrefetchDistance(150)
                .setInitialLoadSizeHint(22)
                .setEnablePlaceholders(true)
                .build();

//        userList = new LivePagedListBuilder<>(userDao.usersById(), 20).build();
        userList = new LivePagedListBuilder<>(userDao.usersById(), myPagingConfig).build();
    }
}
